new Vue({
  el: "#app",
  data() {
    return {
      fileList: [],
      rules: [],
      form: {
        Company_Honors_SerialNumber: "",
        Company_Honors_Name: "",
        Picture_URL: "",
        Company_Honors_Date: "",
      },
      loading: false,
    };
  },
  methods: {
    handleBeforeUpload(file) {
      // 生成预览URL并添加到fileList中
      const reader = new FileReader();
      reader.onload = (e) => {
        //调用存储的接口放在onload中，存在异步操作
        const newFile = {
          name: file.name,
          url: e.target.result, // 预览URL
          uid: file.uid, //添加uid
          // 可以添加其他属性，如上传状态等
        };
        this.fileList.push(newFile);
        console.log(this.fileList);

        // return;
        $.ajax({
          // headers: requestHeaders(), // 确保这个函数返回正确的头部信息
          url: "https://service.kdddy.cn/donghuiForRequest/store/photo",
          // url: "http://192.168.10.79:8087/store/photo",
          type: "POST", // 注意HTTP方法应该大写
          contentType: "application/json", // 设置请求体的媒体类型为application/json
          data: JSON.stringify(this.fileList), // 将数据序列化为JSON字符串
          success: function (Result) {
            console.log(Result);
          },
          dataType: "json", // 预期服务器返回的数据类型
        });
      };
      reader.readAsDataURL(file);
      // 返回false以阻止自动上传

      return false;
    },
    submitForm(formName) {
      if (this.fileList.length > 0) {
        let base64Image = this.fileList[0].url;
        let dataIndex = base64Image.indexOf("data:");
        let base64Index = base64Image.indexOf(";base64,");
        let substring = base64Image.substring(dataIndex + 5, base64Index);
        let ls_split = substring.split("/");
        this.form.Picture_URL = this.fileList[0].uid + "." + ls_split[1];
      }
      console.log(this.form);
      let lArray = new Array();
      lArray.push(this.form);
      let lJSONObject_company_honors = new Object();
      lJSONObject_company_honors.company_honors = lArray; //换表
      let lJSONObject_DataSet = new Object(); // Modify
      lJSONObject_DataSet.Add = lJSONObject_company_honors; //换表
      console.log(lJSONObject_DataSet);
      const _this = this;
      $.ajax({
        // headers: requestHeaders(),
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/donghui/clientOperation/generalSaveForClient",
        type: "POST",
        data: {
          Data: JSON.stringify(lJSONObject_DataSet),
        },
        success: function (Result) {
          console.log(Result);
          if (Result.Status === "success") {
            _this.$message({
              message: "提交成功",
              type: "success",
            });
            window.history.go(-1);
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
      return;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          _this.loading = true;
          _this.form.Company_Honors_Date = getYMDHMSDate();
          $.ajax({
            url: DomainName + "/getAddAction?token=" + $.cookie("token"),
            type: "POST",
            data: {
              Data: JSON.stringify({
                company_honors: [_this.form],
              }),
              Operation_Name: "添加公司资质",
            },
            success: function (res) {
              if (res.Status == "success") {
                _this.$message.success("发布成功!");
                window.history.go(-1);
              }
            },
            error: function () {
              _this.$message.error("操作失败，请稍后再试");
            },
            complete: function (XMLHttpRequest) {
              _this.loading = false;
              if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                window.parent.document.location.href =
                  absolutepath() +
                  XMLHttpRequest.getResponseHeader("CONTENTPATH");
              }
            },
          });
        } else {
          return false;
        }
      });
    },
  },
});
